<template>
    <div class="addgoodlist">
        <div class="addgoodlist_const">
            <!-- 提示信息 -->
            <el-alert title="消息提示的文案" type="info" :closable="false" center show-icon>
            </el-alert>
            <!-- 步骤进度条 -->
            <el-steps :active="active * 1" align-center finish-status="success">
                <el-step title="基本信息"></el-step>
                <el-step title="商品参数"></el-step>
                <el-step title="商品属性"></el-step>
                <el-step title="商品图片"></el-step>
                <el-step title="商品内容"></el-step>
                <el-step title="完成"></el-step>
            </el-steps>
            <!-- 标签页切换 -->
            <el-form :model="form" ref="form" :rules="rules">
                <el-tabs v-model="active" tabPosition="left" @tab-click="handleClick" :before-leave="tabstatus">
                    <el-tab-pane label="基本信息" name="0">
                        <el-form-item label="商品名称" prop="goods_name">
                            <el-input v-model="form.goods_name"></el-input>
                        </el-form-item>
                        <el-form-item type="number" label="商品价格" prop="goods_price">
                            <el-input v-model="form.goods_price"></el-input>
                        </el-form-item>
                        <el-form-item type="number" label="商品重量" prop="goods_weight">
                            <el-input v-model="form.goods_weight"></el-input>
                        </el-form-item>
                        <el-form-item type="number" label="商品数量" prop="goods_number">
                            <el-input v-model="form.goods_number"></el-input>
                        </el-form-item>
                        <div class="block">
                            <span class="demonstration">商品分类: </span>
                            <el-cascader v-model="selectId" :options="options" :props="props" @change="handleChange">
                            </el-cascader>
                        </div>
                    </el-tab-pane>

                    <el-tab-pane label="商品参数" name="1">
                        <el-checkbox-group v-model="manyParams" v-for="(item, index) in manyParams" :key="index">
                            <span>{{ item }}</span>
                            <el-checkbox :label="manyname" v-model="manyParams" v-for="(ele, i) in item.attr_vals"
                                :key="i" checked border>{{ ele }}</el-checkbox>
                        </el-checkbox-group>
                    </el-tab-pane>
                    <el-tab-pane label="商品属性" name="2">
                        <el-form v-for="(item, index) in onlyParams" :key="index">
                            <el-form-item :label="item.attr_name">
                                <el-input v-model="item.attr_vals"></el-input>
                            </el-form-item>
                        </el-form>
                    </el-tab-pane>
                    <el-tab-pane label="商品图片" name="3">
                        <el-upload class="upload-demo" action="http://www.ysqorz.top:8888/api/private/v1/upload"
                            :on-success="handlePreview" :headers="headers" list-type="picture">
                            <el-button size="small" type="primary">点击上传</el-button>
                        </el-upload>
                    </el-tab-pane>
                    <el-tab-pane label="商品内容" name="4">
                        <!-- 富文本编辑器 -->
                        <quill-editor v-model="content"> </quill-editor>
                        <el-button size="small" type="primary" style="margin-top: 16px" @click="AddDoods">添加商品
                        </el-button>
                    </el-tab-pane>
                </el-tabs>
            </el-form>
        </div>
    </div>
</template>

<script>
import { getcategories, getCategories, getAddgood } from "@/utils/http";
export default {
    data() {
        return {
            active: "0",
            // tab切换时的状态
            form: {
                goods_name: "",
                goods_price: "",
                goods_weight: "",
                goods_number: "",
            },
            // 选择器
            selectId: "",
            mygoodsSelectId: "",
            // 动态数据
            manyParams: [],
            manyname: "",
            // 静态数据
            onlyParams: [],
            options: [],
            // 上传图片
            mytmp_path: "",
            // 富文本内容
            content: "",
            headers: {
                Authorization: localStorage.getItem("token"),
            },
            props: { expandTrigger: "hover", value: "cat_id", label: "cat_name" },
            rules: {
                goods_name: [
                    { required: true, message: "请输入名称", trigger: "blur" },
                ],
                goods_price: [
                    { required: true, message: "请输入价格", trigger: "blur" },
                ],
                goods_weight: [
                    { required: true, message: "请输入重量", trigger: "blur" },
                ],
                goods_number: [
                    { required: true, message: "请输入数量", trigger: "blur" },
                ],
            },
        };
    },
    created() {
        getcategories().then((res) => {
            console.log(res);
            this.options = res.data;
        });
    },
    methods: {
        // 点击添加商品
        AddDoods() {
            let obj = {
                ...this.form,
                goods_cat: this.mygoodsSelectId,
            };
            getAddgood(obj).then((res) => {
                console.log(res);
                if (res.meta.status === 201) {
                    this.$message({
                        message: "添加成功",
                        type: "success",
                    });
                } else if (res.meta.status != 200) {
                    this.$message.error("错了哦: " + res.meta.msg);
                }
            });
            this.$router.push("/goods");
        },
        // 图片上传成功数据
        handlePreview(file) {
            this.mytmp_path = file.data.tmp_path;
        },
        // 动态数据
        handleClick() {
            if (this.active == "1") {
                getCategories({ id: this.selectId, sel: "many" }).then((res) => {
                    console.log(res);
                    res.data.forEach((ele) => {
                        ele.attr_vals =
                            ele.attr_vals.length > 0 ? ele.attr_vals.split(",") : [];
                        this.manyParams = res.data;
                        console.log(this.manyParams);
                    });
                });
            }
            if (this.active == "2") {
                getCategories({ id: this.selectId, sel: "only" }).then((res) => {
                    this.onlyParams = res.data;
                    // console.log(res);
                });
            }
        },
        // 判断基本信息 输入框是否都填写
        tabstatus(a, b) {
            if (a == 1 && this.selectId == "") {
                this.$message.error("请填写对信息");
                return false;
            }
            for (const i in this.form) {
                if (this.form[i] === "") {
                    this.$message.error("请填写对信息");
                    return false;
                }
            }
        },
        // 选着器
        handleChange(val) {
            this.mygoodsSelectId = val.join(",");
            // console.log(this.mygoodsSelectId);
            this.selectId = val[2];
        },
    },
};
</script>

<style lang="scss" scoped>
::v-deep .ql-editor {
    height: 320px;
}

.addgoodlist {
    background: white;
    border-radius: 8px;

    .addgoodlist_const {
        padding: 15px;

        ::v-deep .el-steps {
            margin-top: 15px;

            .el-step__title {
                font-size: 15px;
            }
        }

        .el-tabs {
            margin-top: 15px;
        }
    }
}
</style>